// Deflater.cs
//
// Copyright (C) 2001 Mike Krueger
// Copyright (C) 2004 John Reilly
// Copyright (C) 2010 Thomas Maierhofer
//
// (2010) This Code was modified from SharpZipLib http://www.icsharpcode.net/OpenSource/SharpZipLib/  
// to fit the needs of Second WAF - a heavy load web application framework 
//
// This file was translated from java, it was part of the GNU Classpath
// Copyright (C) 2001 Free Software Foundation, Inc.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// Linking this library statically or dynamically with other modules is
// making a combined work based on this library.  Thus, the terms and
// conditions of the GNU General Public License cover the whole
// combination.
// 
// As a special exception, the copyright holders of this library give you
// permission to link this library with independent modules to produce an
// executable, regardless of the license terms of these independent
// modules, and to copy and distribute the resulting executable under
// terms of your choice, provided that you also meet, for each linked
// independent module, the terms and conditions of the license of that
// module.  An independent module is a module which is not derived from
// or based on this library.  If you modify this library, you may extend
// this exception to your version of the library, but you are not
// obligated to do so.  If you do not wish to do so, delete this
// exception statement from your version.



namespace WAF.Content
{
    using System;	
	/// <summary>
	/// This is the Deflater class.  The deflater class compresses input
	/// with the deflate algorithm described in RFC 1951.  It has several
	/// compression levels and three different strategies described below.
	///
	/// This class is <i>not</i> thread safe.  This is inherent in the API, due
	/// to the split of deflate and setInput.
	/// 
	/// author of the original java version : Jochen Hoenicke
	/// </summary>
	public partial class Deflater
	{
        public delegate void StoreDataDelegate(byte[] buffer, int bufferLength, object context);

		public Deflater()
		{
            // Output Buffer
            this.outputBufferSize = -1;
            outputBuffer = null;

            // Deflate Compressor
            window = new byte[2 * WSIZE];
            patternHashtable = new ushort[HASH_SIZE];
            previousPattern = new ushort[WSIZE];
            // We start at index 1, to avoid an implementation deficiency, that
            // we cannot build a repeat pattern at index 0.
            blockStart = windowStart = 1;


            // Huffman encoding
            literalTree = new HuffmanTree(this, STATIC_LITERAL_CODES_NUMBER, 257, 15);
            distanceTree = new HuffmanTree(this, STATIC_DISTANCE_CODES_NUMBER, 1, 15);
            bitLengthTree = new HuffmanTree(this, BITLEN_NUM, 4, 7);

            distanceBuffer = new short[BUFSIZE];
            literalBuffer = new byte[BUFSIZE];

        }
		
		public void Reset(DeflaterConfig config)
		{
            if (config == null)
                throw new ArgumentNullException("config");
            
            if (config.StoreDataDelegate == null)
                throw new ArgumentNullException("config.StoreDataDelegate");

            if (config.OutputBufferSize < Deflater.MIN_OUTPUT_BUFFER_SIZE)
                throw new ArgumentOutOfRangeException("config.OutputBufferSize", "Is smaller than" + Deflater.MIN_OUTPUT_BUFFER_SIZE);
            
            
            // Data Storage
            this.StoreData = config.StoreDataDelegate;
            this.storeDataContext = config.StoreDataContext;

            // Output Buffer
            if (this.outputBuffer == null || this.outputBufferSize != config.OutputBufferSize)
            {
                this.outputBufferSize = config.OutputBufferSize;
                outputBuffer = new byte[config.OutputBufferSize];
            }

            outputBufferStart = 0;
            outputBufferEnd = 0;
            outputBufferBitCount = 0;

            // Performance and Size Constants
            if (config.FastCompression)
            {
                this.fastCompression = true;
                this.goodMatchLength = 4; // 4-32 DeflaterConstants.GOOD_LENGTH[1];
                this.max_lazy = 4; // 4-258  DeflaterConstants.MAX_LAZY[1];
                this.niceLength = 8; // 8-258  DeflaterConstants.NICE_LENGTH[1];
                this.max_chain = 4; // 4-4096  DeflaterConstants.MAX_CHAIN[1];
            }


            // Deflate Compressor
            blockStart = windowStart = 1;
            windowLookahead = 0;
            previousByteAvailable = false;
            matchLen = MIN_MATCH - 1;

            for (int i = 0; i < HASH_SIZE; i++)
            {
                patternHashtable[i] = 0;
            }

            for (int i = 0; i < WSIZE; i++)
            {
                previousPattern[i] = 0;
            }

            HuffmanReset();

            this.dataFormat = config.Format;

            switch (this.dataFormat)
            {
                case DeflaterConfig.StreamFormat.GZIP:
                    crc32 = 0xFFFFFFFF; 
                    originalSize = 0;

                    // Write a GZIP (RFC 1952) Header
                    if (this.fastCompression)
                        OutputBufferWriteSymbol(staticGZipHeaderFast, staticGZipHeaderFast.Length);
                    else
                        OutputBufferWriteSymbol(staticGZipHeaderSlow, staticGZipHeaderSlow.Length);
                    break;                    
                case DeflaterConfig.StreamFormat.ZLIB:
                    checksumAdler32S1 = 1;
                    checksumAdler32S2 = 0;

                    // Write a ZLIB (RFC 1950) Header
                    if( this.fastCompression )
                        OutputBufferWriteSymbol(staticZLibHeaderFast, staticZLibHeaderFast.Length);
                    else
                        OutputBufferWriteSymbol(staticZLibHeaderSlow, staticZLibHeaderSlow.Length);
                    break;
            }

		}

	

	

	}
}
